package nc.bs.bdf.maindate;

import java.util.HashMap;
import java.util.Map;

import nc.bs.bd.CorpDMO;
import nc.bs.bd.b04.DeptdocImp;
import nc.bs.bdf.BdfDMO;
import nc.bs.bdf.IMainDate;
import nc.bs.framework.common.NCLocator;
import nc.bs.om.om_001.CorpctrlImpl;
import nc.ebs.basedata.webservice.GetUserInfos;
import nc.impl.uap.bd.corp.CorpImpl;
import nc.impl.uap.bd.cust.CubasdocImpl;
import nc.itf.bdf.IBdf;
import nc.pub.zip.Constant;
import nc.pub.zip.bs.DAOTools;
import nc.pub.zip.bs.DOCTools;
import nc.vo.bd.CorpVO;
import nc.vo.bd.b04.DeptdocVO;
import nc.vo.bd.b08.CubasdocVO;
import nc.vo.bd.b38.JobbasfilVO;
import nc.vo.bdf.bdf_d1h3.SyncLogVO;
import nc.vo.bdf.bdf_d1hc.CustBasVO;

import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;

public class InsertMD implements IMainDate {

	public String executeUpdate(Object param) throws BusinessException {
		// TODO Auto-generated method stub
		Map m = (Map) param;
		String type = (String) m.get("mdtype");
		String action = (String) m.get("action");
		SuperVO vo = (SuperVO) m.get("vo");
		String pk_md= vo.getPrimaryKey();
		vo.setPrimaryKey(null);
		String ret = null;
		try {
			if (Constant.MD_TABLE_DEPT.equals(type)) {
				DeptdocVO deptvo = (DeptdocVO) vo;
				ret =new nc.bs.bd.b04.DeptdocImp().insert(deptvo);
			} else if (Constant.MD_TABLE_CUST.equals(type)) {
				CustBasVO myCustvo = (CustBasVO) vo;
				 CubasdocVO custVO= new CubasdocVO();				 
				 custVO.setCustBasVO(DOCTools.getCubasdocVObyVO(myCustvo)); 
				 ret = new nc.impl.uap.bd.cust.CubasdocImpl().insertCust(custVO, true);
				 if(ret!=null){
					Map newParam = new HashMap();
					newParam.put("pk_md", pk_md);
					newParam.put("pk_bd", ret);
					newParam.put("action", action);
					NCLocator.getInstance().lookup(IBdf.class).executeUpdateMD(newParam, Constant.MD_ACTION_CUSTBANK);
				 }
			}else if(Constant.MD_TABLE_GS.equals(type)){
				CorpVO corpVO = (CorpVO)vo;
				ret = new  nc.impl.uap.bd.corp.CorpImpl().insert(corpVO);
				
			}else if (Constant.JOBBAS_YZ.equals(type)) {
				JobbasfilVO jobbasfilVO = (JobbasfilVO) vo;
				ret =new nc.bs.bd.b38.JobbasfilDMO().insert(jobbasfilVO);
			} else if (Constant.JOBBAS_YT.equals(type)) {
				JobbasfilVO jobbasfilVO = (JobbasfilVO) vo;
				ret =new nc.bs.bd.b38.JobbasfilDMO().insert(jobbasfilVO);
			} else if (Constant.JOBBAS_PP.equals(type)) {
				JobbasfilVO jobbasfilVO = (JobbasfilVO) vo;
				ret =new nc.bs.bd.b38.JobbasfilDMO().insert(jobbasfilVO);
			} else if (Constant.JOBBAS_LC.equals(type)) {
				JobbasfilVO jobbasfilVO = (JobbasfilVO) vo;
				ret =new nc.bs.bd.b38.JobbasfilDMO().insert(jobbasfilVO);
			} else if (Constant.JOBBAS_XD.equals(type)) {
				JobbasfilVO jobbasfilVO = (JobbasfilVO) vo;
				ret =new nc.bs.bd.b38.JobbasfilDMO().insert(jobbasfilVO);
			}
			
			
			insertlogs(vo, UFBoolean.TRUE, null, action, type);
		} catch (Exception e) {
			insertlogs(vo, UFBoolean.FALSE, e.getMessage(), action, type);
			e.printStackTrace();
			throw new BusinessException(e.getMessage());
		}

		return ret;
	}

	public void insertlogs(SuperVO vo, UFBoolean result, String failreason, String action, String type) throws BusinessException {
		BdfDMO bdfDMO = new BdfDMO();
		SyncLogVO logVO = bdfDMO.setlogVO(vo, result, failreason, action, type);
		try {
			bdfDMO.writeLog(logVO);
		} catch (Exception ex) {
			throw new BusinessException(ex.getMessage());
		}
	}

	//	

}
